<template>
  <div>
    <el-dialog :visible.sync='isShow' title='绑定交易组' width="800px" @open="openDialogInit" @close="resetForm" :close-on-click-modal=false>
      <div v-loading="loading">
        <el-card>
          <el-form :model="formInline" ref="formInline" size="mini" :rules="rules" label-position="left" label-width="125px">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="机构组编号:" prop="branchgroupnum">
                  <el-input v-model="formInline.branchgroupnum" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="机构组名称:" prop="branchgroupname">
                  <el-input v-model="formInline.branchgroupname" disabled></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="法人号:" prop="LegalPersonNum">
                  <el-input v-model="formInline.legalpersonnum" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人名称:" prop="LegalPersonName">
                  <el-input v-model="formInline.LegalPersonName" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label='应用编号' prop='AppNum'>
                  <el-select v-model="formInline.AppNum" @change="AppNumChange" placeholder="请选择应用编号">
                    <el-option v-for="item in appQueryList" :key="item.AppNum" :label="item.AppNum + '-' + item.AppName" :value="item.AppNum">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-card style="margin-top:10px;max-height:300px;overflow:auto;">
          <el-tree :data="pageList" ref="tree" :props="props" show-checkbox :default-checked-keys="['AWEB']" node-key="trangroupid" @check-change="handleCheckChange">
          </el-tree>
        </el-card>
        <el-row style="margin:20px 0;text-align:right">
          <el-button size="mini" @click="isShow=false">取消</el-button>
          <el-button type="primary" size="mini" @click="branchGroupTran">确认</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/common/utils/request";
import appQuery from "@/message/shared/appquery";
import branchGroup from "@/message/branchManage/branchGroupMgr/branch-group-mgr";
import branchGroupTran from "@/message/branchManage/branchGroupMgr/branch-group-tran";
export default {
  props: { option: Object },
  data() {
    return {
      loading: false,
      formInline: {},
      pageList: [],
      pageList1: [],
      PostList: [],
      appQueryList: [],
      props: {
        id: "trangroupid",
        label: "trangroupname",
        children: "children"
      },
      rules: {
        AppNum: [
          { required: true, message: "请选择应用编号", trigger: "change" }
        ]
      }
    };
  },
  computed: {
    isShow: {
      get: function() {
        return this.option.isShow;
      },
      set: function(newValue) {
        this.option.isShow = newValue;
      }
    }
  },
  methods: {
    // 打开窗口初始化
    openDialogInit() {
      
      this.appQueryInit();
      this.$nextTick(()=>{

        this.$refs["formInline"].clearValidate();
      })
    },

    // 设备应用下拉框初始化查询
    appQueryInit() {
      let resBody = new appQuery();
      resBody.data.UserNum = this.$store.getters.UserNum;
      request(resBody)
        .then(response => {
          this.formInline = JSON.parse(JSON.stringify(this.option.currentRow));
      
      this.formInline.legalpersonnum = this.$store.getters.LegalPersonNum;
      this.formInline.LegalPersonName = this.$store.getters.LegalPersonName;
          // this.formInline = this.option.currentRow;
          this.appQueryList = response.RspInfo.Result;
          this.formInline.AppNum = response.RspInfo.Result[0].AppNum;
          this.branchGroupTranQuery(this.formInline.AppNum);
        })
        .catch(error => {
          console.error("查询错误", error);
        });
    },

    // 机构组交易查询
    branchGroupTranQuery(val) {
      let resBody = new branchGroupTran();
      resBody.data.BranchGroupNum = this.formInline.branchgroupnum;
      resBody.data.LegalPersonNum = this.formInline.legalpersonnum;
      resBody.data.AppNum = val;
      resBody.data.OperType = "Q"; // 操作类型
      request(resBody)
        .then(response => {
          if (response.SYS_HEAD.ReturnCode === "000000") {
            let pageList = response.RspInfo.pageList || [];
            this.pageList1 = response.RspInfo.pageList1 || [];
            let pageList2 = response.RspInfo.pageList2 || [];
            if (pageList2.length == 0) {
              this.$message({
                type: "warning",
                message: "该应用下无交易"
              });
              this.pageList = [];
              return;
            }
            this.setResposeList(pageList, pageList2);
          } else {
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
          }
        })
        .catch(error => {
          console.error("查询错误", error);
        });
    },

    // 返回值树结构处理
    setResposeList(pageList, pageList2) {
      let trangroupid = {
        trangroupid: pageList2[0].appnum || "",
        trangroupname: pageList2[0].appnum || "",
        children: []
      };
      trangroupid = Object.assign(pageList2[0], trangroupid);
      trangroupid.children = pageList;
      this.$set(this.pageList, 0, trangroupid);
      let temp = [];
      for (let i = 0; i < this.pageList1.length; i++) {
        const element = this.pageList1[i].privlegeid;
        temp.push(element);
      }
      this.pageList1 = JSON.parse(JSON.stringify(temp));
      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys(this.pageList1);
      });
    },

    // 设备应用值改变事件
    AppNumChange(val) {
      this.branchGroupTranQuery(val);
    },
    // 树值改变事件
    handleCheckChange(data, checked, indeterminate) {
      let keyList = this.$refs.tree.getCheckedKeys();
      this.PostList = [];
      for (let i = 0; i < keyList.length; i++) {
        const element = keyList[i];
        let PostList = {
          TranGrpId: element
        };
        if (this.formInline.AppNum != element) {
          this.PostList.push(PostList);
        }
      }
    },

    // 重置表单
    resetForm() {
      this.$refs["formInline"].resetFields();
      this.$refs["formInline"].clearValidate();
    },

    /**
     * @desc 交易组绑定维护
     *
     */
    branchGroupTran() {
      this.$refs["formInline"].validate(valid => {
        if (valid) {
          this.loading = true;
          let resBody = new branchGroupTran();
          resBody.data.BranchGroupNum = this.formInline.branchgroupnum;
          resBody.data.LegalPersonNum = this.formInline.legalpersonnum;
          resBody.data.AppNum = this.formInline.AppNum;
          resBody.data.PostList = this.PostList;
          resBody.data.OperType = "M"; // 操作类型
          request(resBody)
            .then(response => {
              if (response.SYS_HEAD.ReturnCode === "000000") {
                this.loading = false;
                this.$message({
                  type: "success",
                  message: "绑定成功!"
                });
                this.isShow = false;
                this.$emit("refreshBrand");
              } else {
                this.loading = false;
                this.$message({
                  type: "error",
                  message: response.SYS_HEAD.ReturnMessage
                });
              }
            })
            .catch(error => {
              this.loading = false;
              console.log(error);
            });
        } else {
          this.$message({
            type: "warning",
            message: "必输项不能为空"
          });
          return false;
        }
      });
    }
  }
};
</script>
<style lang="sass" scoped>

</style>
